package com.diuse.outer.executor;


import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//import org.apache.http;

/**
 * Created by Administrator on 2015/11/3 0003.
 */

public class SingletonHttpClient {

    private static SingletonHttpClient httpClient;
    public static SingletonHttpClient getInstance(){
        if (httpClient == null) {
            httpClient = new SingletonHttpClient();
        }
        return httpClient;
    }
    public static void main(String[] args){
        try {
            String loginUrl = "http://www.diusestudio.com:8080/pingba/pingba/user/login";
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("phone", "18767121724"));
            params.add(new BasicNameValuePair("passwd", "12121212"));

            new SingletonHttpClient().requestPost(loginUrl, params);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void requestPost(String url,List<NameValuePair> params) throws IOException {
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();

        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(params));
        CloseableHttpResponse response = httpclient.execute(httppost);
        System.out.println(response.toString());
        HttpEntity entity = response.getEntity();
        String jsonStr = EntityUtils.toString(entity, "utf-8");
        System.out.println(jsonStr);
        httppost.releaseConnection();
    }



}